/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef BUS_HISPORT_H
#define BUS_HISPORT_H

#include "internal/bus/bus_base/bus.h"
#include "internal/hasdef.h"
#include <mc/engine.h>
#include <memory>
#include <string>

#include "drivers/hisport.h"

namespace dev {

constexpr uint8_t  MAX_OFFSET_WIDTH         = 4;
constexpr uint8_t  DEFAULT_BUS_NUM          = 128;
constexpr uint8_t  DEFAULT_REG_NUM          = 10;
constexpr uint8_t  DEFAULT_FLAG_NUM         = 2;
constexpr uint8_t  DEFAULT_SEND_REG_ADDR    = 0x00;
constexpr uint8_t  DEFAULT_RECV_REG_ADDR    = 0x10;
constexpr uint8_t  DEFAULT_FLAG_REG_ADDR    = 0x1A;
constexpr uint8_t  DEFAULT_MAX_BATCH_LEN    = 2;
constexpr uint32_t DEFAULT_MAX_DATA_LEN     = 1024;
constexpr uint8_t  DEFAULT_MAX_OFFSET_WIDTH = 2;
constexpr uint8_t  DEFAULT_MAX_LENGTH_WIDTH = 2;
constexpr uint8_t  DEFAULT_RE_WRITE_CNT     = 10;
constexpr uint8_t  DEFAULT_RE_READ_CNT      = 10;
constexpr uint32_t HISPORT_EXTEND_READ_MODE = 0xffffffff;

class bus_hisport : public bus_base {
public:
    bool init(mc::mutable_dict& csr_object);

    std::unique_ptr<DRIVER_HISPORT::Hisport> m_hisport_drv;

    void drv_init(DRIVER_HISPORT::Hisport* m_hisport_drv);
    // 实现虚函数
    uint32_t read(ACCESS_DATA_S& input) override;
    uint32_t write(ACCESS_DATA_S& input) override;
    uint8_t  get_id() override;
    void     bus_lock() override;
    void     bus_unlock() override;

private:
    uint8_t  m_id;
    uint8_t  m_bus_num          = DEFAULT_BUS_NUM;
    uint8_t  m_reg_num          = DEFAULT_REG_NUM;
    uint8_t  m_flag_num         = DEFAULT_FLAG_NUM;
    uint8_t  m_send_reg_addr    = DEFAULT_SEND_REG_ADDR;
    uint8_t  m_recv_reg_addr    = DEFAULT_RECV_REG_ADDR;
    uint8_t  m_flag_reg_addr    = DEFAULT_FLAG_REG_ADDR;
    uint8_t  m_max_batch_len    = DEFAULT_MAX_BATCH_LEN;
    uint32_t m_max_data_len     = DEFAULT_MAX_DATA_LEN;
    uint8_t  m_max_offset_width = DEFAULT_MAX_OFFSET_WIDTH;
    uint8_t  m_max_length_width = DEFAULT_MAX_LENGTH_WIDTH;
    uint8_t  m_re_write_cnt     = DEFAULT_RE_WRITE_CNT;
    uint8_t  m_re_read_cnt      = DEFAULT_RE_READ_CNT;
};

} // namespace dev

#endif // BUS_HISPORT_H
